package cn.ac.siom.query.action;

import cn.ac.siom.query.util.action.DefaultAction;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/home/*")
public class GetIP extends DefaultAction {
    public static Log log=LogFactory.getLog(GetIP.class);

    @ResponseBody
    @RequestMapping(value = "getIpAddr", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public boolean getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        log.debug("获取到的IP地址"+ip);
        System.out.println("获取到的IP地址"+ip);
        //ip="202.127.20.56";
        //ip="223.167.150.175";
        //ip="172.16.150.175";
        String ip1="210.72.9.*";
        String ip2="159.226.130.*";
        String ip3="210.72.70.*";
        String ip4="119.78.236.*";
        String ip5="119.78.237.*";
        String ip6="119.78.238.*";
        String ip7="119.78.239.*";
        String ip8="223.167.150.*";
        String ip9="172.16.*.*";
        String[] ips = ip.split("\\.");
        String[] ip1s = ip1.split("\\.");
        String[] ip2s = ip2.split("\\.");
        String[] ip3s = ip3.split("\\.");
        String[] ip4s = ip4.split("\\.");
        String[] ip5s = ip5.split("\\.");
        String[] ip6s = ip6.split("\\.");
        String[] ip7s = ip7.split("\\.");
        String[] ip8s = ip8.split("\\.");
        String[] ip9s = ip9.split("\\.");
      /**  int ipAddr = (Integer.parseInt(ips[0]) )
                | (Integer.parseInt(ips[1]) )
                | (Integer.parseInt(ips[2]) )
                | Integer.parseInt(ips[3]);
        int ipAddr1 = (Integer.parseInt(ip1s[0]) )
                | (Integer.parseInt(ip1s[1]) )
                | (Integer.parseInt(ip1s[2]) )
                | Integer.parseInt(ip1s[3]);
        int ipAddr2 = (Integer.parseInt(ip2s[0]) )
                | (Integer.parseInt(ip2s[1]) )
                | (Integer.parseInt(ip2s[2]) )
                | Integer.parseInt(ip2s[3]);
        System.out.println(ipAddr1);
        System.out.println(ipAddr2); **/
        String ipAddr = ips[0]+ips[1]+ips[2];
        String ipAddr0 = ips[0]+ips[1];
        //int ipAddr00 =Integer.parseInt(ipAddr);
        System.out.println(ipAddr);
        String ipAddr1 = ip1s[0]+ip1s[1]+ip1s[2];
        //int ipAddr100 =Integer.parseInt(ipAddr1);
        System.out.println(ipAddr1);
        String ipAddr2 = ip2s[0]+ip2s[1]+ip2s[2];
        //int ipAddr200 =Integer.parseInt(ipAddr2);
        String ipAddr3 = ip3s[0]+ip3s[1]+ip3s[2];
        String ipAddr4 = ip4s[0]+ip4s[1]+ip4s[2];
        String ipAddr5 = ip5s[0]+ip5s[1]+ip5s[2];
        String ipAddr6 = ip6s[0]+ip6s[1]+ip6s[2];
        String ipAddr7 = ip7s[0]+ip7s[1]+ip7s[2];
        String ipAddr8 = ip8s[0]+ip8s[1]+ip8s[2];
        String ipAddr9 = ip9s[0]+ip9s[1];
        System.out.println(ipAddr2);
        //Map map=new HashMap<>();
        if (ipAddr.equals(ipAddr1)  || ipAddr.equals(ipAddr2) || ipAddr.equals(ipAddr3) || ipAddr.equals(ipAddr4)|| ipAddr.equals(ipAddr5)|| ipAddr.equals(ipAddr6)|| ipAddr.equals(ipAddr7) || ipAddr.equals(ipAddr8) || ipAddr0.equals(ipAddr9)){
            //map.put("ip",ip);
            //map.put("name",true);
            return true;
        }else{
            //map.put("ip",ip);
            //map.put("name",true);
            return false;
        }

    }

    @Override
    public String getText() {
        return null;
    }
}

